package com.hongbo.echartsjavawebdemo.echarts.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.hongbo.echartsjavawebdemo.echarts.service.IEchartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Controller
@RequestMapping("/echarts/dyndemo")
public class EchartsDynController {

    @Autowired
    private IEchartService echartService;

    @GetMapping("/DynBarPieDemo")
    @ResponseBody
    public ModelAndView  echart(){
        ModelAndView modal = new ModelAndView();
        //实际跳转到的前台页面
        modal.setViewName("/echarts/dyndemo/DynBarPieDemo");
        String optionStr  = null;
        //禁用引用对象
//        optionStr = JSON.toJSONString(echartService.selectRemoveCauses(),
//                                        SerializerFeature.PrettyFormat,
//                                        SerializerFeature.DisableCircularReferenceDetect);
        optionStr = JSON.toJSONString(echartService.weekLoginCount(),
                                SerializerFeature.PrettyFormat,
                                SerializerFeature.DisableCircularReferenceDetect);
//        log.info("\n"+optionStr);
        modal.addObject("saleData",JSON.parseObject(optionStr));

//        //禁用引用对象
        optionStr = JSON.toJSONString(echartService.getLoginPie(),
                                        SerializerFeature.PrettyFormat,
                                        SerializerFeature.DisableCircularReferenceDetect);

        JSONObject jsonObject = JSON.parseObject(optionStr);
        String labelStr = "{\n" +
                "                        show:true,\n" +
                "                        formatter: '{a|{a}}{abg|}\n{hr|}\n  {b|{b}：}{c}  {per|{d}%}  ',\n" +
                "                        backgroundColor: '#eee',\n" +
                "                        borderColor: '#aaa',\n" +
                "                        borderWidth: 1,\n" +
                "                        borderRadius: 4,\n" +
                "                        rich: {\n" +
                "                            a: {\n" +
                "                                color: '#999',\n" +
                "                                lineHeight: 22,\n" +
                "                                align: 'center'\n" +
                "                            },\n" +
                "                            hr: {\n" +
                "                                borderColor: '#aaa',\n" +
                "                                width: '100%',\n" +
                "                                borderWidth: 0.5,\n" +
                "                                height: 0\n" +
                "                            },\n" +
                "                            b: {\n" +
                "                                fontSize: 16,\n" +
                "                                lineHeight: 33\n" +
                "                            },\n" +
                "                            per: {\n" +
                "                                color: '#eee',\n" +
                "                                backgroundColor: '#334455',\n" +
                "                                padding: [2, 4],\n" +
                "                                borderRadius: 2\n" +
                "                            }\n" +
                "                        \n" +
                "                        }\n" +
                "                    }";
        jsonObject.getJSONArray("series").getJSONObject(0)
                .getJSONObject("itemStyle").getJSONObject("normal")
                .put("label", JSON.parseObject(labelStr));

//        log.info(JSON.toJSONString(jsonObject,SerializerFeature.PrettyFormat));
        modal.addObject("option",jsonObject);
        return modal;

    }

    @GetMapping("/DynBarLinePieDemo")
    @ResponseBody
    public ModelAndView dynBarLinePie() {
        ModelAndView modal = new ModelAndView();
        //实际跳转到的前台页面
        modal.setViewName("/echarts/dyndemo/DynBarLinePieDemo");

        return modal;
    }

    @GetMapping("/getDynBarSeriasData")
    @ResponseBody
    public JSONArray getDynBarSeriasData(){
        JSONArray jsonArray = null;

        Random random = new Random();
        List list = new ArrayList();
        for(int i=0; i<6; i++){
            list.add(random.nextInt(100));
        }

        jsonArray = JSON.parseArray( JSON.toJSONString(list));
        log.info(jsonArray.toJSONString());

        return jsonArray;
    }

    //随机产生dataNumber个整数，并使其和为dataSum
    @PostMapping("/getDynPieSeriasData")
    @ResponseBody
    public  JSONArray getDynPieSeriasData(Integer dataNumber, Integer dataSum){
        JSONArray jsonArray = null;
        log.info("dataNumber"+dataNumber+" dataSum"+dataSum);

        Random random = new Random();
        List list = new ArrayList();

        int sum = dataSum;
        int ran;
        for(int i=0; i<(dataNumber-1); i++){
            ran = random.nextInt(sum);
//            sum = sum-ran;
            sum -= ran;
            list.add(ran);
        }
        list.add(sum);


        jsonArray = JSON.parseArray( JSON.toJSONString(list));
        log.info(jsonArray.toJSONString());

        return jsonArray;
    }


}
